//
//  HMQuestionViewController.m
//  G05爱鲜蜂
//
//  Created by 小灰灰 on 16/10/19.
//  Copyright © 2016年 何源. All rights reserved.
//

#import "HMQuestionViewController.h"
#import "HMHelpModel.h"
#import "HMHeaderView.h"
static NSString *ide = @"HMQuestionViewController";
@interface HMQuestionViewController ()<UITableViewDelegate,UITableViewDataSource,HMHeaderViewDelegate>
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)BOOL isOpen;
@end

@implementation HMQuestionViewController
-(NSArray *)dataArray{
    if(_dataArray == nil){
        _dataArray =[NSArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //处理数据
    self.navigationItem.title = @"常见问题";
    NSString *path = [[NSBundle mainBundle]pathForResource:@"HelpPlist" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *muArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        HMHelpModel *model = [HMHelpModel new];

       [model yy_modelSetWithDictionary:dic];
        [muArr addObject:model];
    }
    
    self.dataArray = muArr;
    //设置tabelView?????????????????
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView = tableView;
    [self.view addSubview:tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ide];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.sectionHeaderHeight = 50;
    //添加这两行自动计算行高
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    HMHelpModel *model = self.dataArray[section];
    return  model.open? model.texts.count:0;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ide forIndexPath:indexPath];
    HMHelpModel *model = self.dataArray[indexPath.section];
    cell.textLabel.text = model.texts[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = [UIColor lightGrayColor];
    cell.textLabel.numberOfLines = 0;
    return cell;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return self.view;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    NSString *reuseId = @"headerView";
    
    //通过这个方法,完成头尾view的重用
   
   HMHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuseId];
    
    if (!headerView) {
        headerView = [[HMHeaderView alloc]initWithReuseIdentifier:reuseId];
    }
    HMHelpModel *model = self.dataArray[section];
    
    headerView.model = model;
    
    
    
    
#pragma mark -- 使用代理: 1. 设置代理对象
    headerView.delegate = self;
 
    return headerView;
}
-(void)btnDidClickHeaderView:(HMHeaderView *)header
{
    header.model.open = !header.model.open;
    [self.tableView reloadData];
    //header.model.open = self.isOpen;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;;
}
//去掉headerView黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 64;
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}
//计算行高
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 80;
//}
@end
